<?php

namespace app\api\controller;

use alibaba\Check;
use app\common\controller\Api;
use fast\Ecar;
use think\Db;
use think\exception\PDOException;
use wxApp\WxApp;

/**
 * 圈子接口
 */
class Circle extends Api
{

    protected $noNeedLogin = ['detail'];
    protected $noNeedRight = ['*'];

    //圈子列表
    public function list()
    {

        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");



        if (empty($page)) {
            $page = 1;
        }
        $offset = ($page - 1) * $limit;
        $list = Db::name("circle_info");
        $type = input("type");
        if (!empty($type) && $type > 0) {
            $list = $list->where("c.type",$type);

        }

        //所在地
        $param= input("get.city/a");
        if (!empty($param)) {

            $val = implode("|", $param);

            $list = $list->where("c.city REGEXP '[,|;]?".$val."[,|;]?'");

//            foreach ($param as $v) {
//                if (!empty($v)) {
//                    $list = $list->whereOr('c.city',"like",$v."%");
//
//                }
//            }
        }

        $user_id = input("user_id");
        if (!empty($user_id)) {
            $list = $list->where("c.user_id",$user_id);
        }

        $quick_search = input("quick_search");
        if (!empty($quick_search)) {
            $list = $list->where("c.car_brand_name|c.car_series_name|c.car_models_name|c.info","like","%$quick_search%");

        }

        if ($this->auth->id == $user_id) {
            $status = input("status");
            if ($status != "") {
                $list = $list->where("c.status", $status);
            }
        } else {
            $list = $list->where("c.status", 1);
        }


        $list = $list->alias("c")
            ->field("c.*")
            ->field("u.nickname,u.avatar,u.mobile")
            ->join('user u','u.id = c.user_id')
            ->order("id","desc")
            ->limit("$offset,$limit")
            ->select();

        if ($this->auth->id != $user_id) {
            foreach ($list as $k=>$v) {

                $lista = Db::name("circle_comment")
                    ->alias("a")
                    ->field("a.*,u.nickname as user_nickname,u.avatar as user_avatar")
                    ->join('user u', 'u.id = a.user_id')
                    ->where("circle_info_id", $v["id"])
                    ->where("a.pid", 0)
                    ->limit(2)
                    ->order("a.id desc")->select();

                $list[$k]["comment"] = $lista;

            }
        }




        $this->success('请求成功', $list);

    }

    //圈子发布
    public function add()
    {

        $params = input("post.");
        $params["status"] = 1;
        $params["verify_status"] = 1;
        $params["createtime"] = time();
        $params["user_id"] = $this->auth->id;


        //车辆图片
        if (!empty($params["car_image"])) {
            $car_images =  explode(",", $params["car_image"]);
            foreach ($car_images as $k=>$v ) {
                $req = (new Check)->imageCheck("https://oss.taochewa.com".$v);
                if (!empty($req)) {
                    $i = $k+1;
                    $this->error("车辆图片第".$i."张，存在违规");
                }
            }
        }



        //文字检验
        if (!empty($params["info"])) {
            $req = (new Check)->textCheck($params["info"]);
            if (!empty($req)) {
                $this->error("描述，存在”".$req."“内容");
            }
        }



        $result = false;
        Db::startTrans();
        try {

            $result = Db::name("circle_info")->insert($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success("发布成功");

    }

    //圈子发布
    public function edit()
    {

        $params = input("post.");
        $id = $params["id"];
        unset($params["id"]);
        if (empty($id)){
            $this->error("id不能为空");
        }
        //车辆图片
        if (!empty($params["car_image"])) {
            $car_images =  explode(",", $params["car_image"]);
            foreach ($car_images as $k=>$v ) {
                $req = (new Check)->imageCheck("https://oss.taochewa.com".$v);
                if (!empty($req)) {
                    $i = $k+1;
                    $this->error("车辆图片第".$i."张，存在违规");
                }
            }
        }



        //文字检验
        if (!empty($params["info"])) {
            $req = (new Check)->textCheck($params["info"]);
            if (!empty($req)) {
                $this->error("描述，存在”".$req."“内容");
            }
        }



        $result = false;
        Db::startTrans();
        try {

            $result = Db::name("circle_info")->where("id",$id)->update($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success("发布成功");

    }

    //圈子详情
    public function detail()
    {

        $params = input("id");
        $result = Db::name("circle_info")
            ->alias("c")
            ->field("c.*")
            ->field("u.nickname,u.avatar")
            ->join('user u','u.id = c.user_id')
            ->where("c.id", $params)
            ->find();
        if ($result["user_id"] == $this->auth->id) {
            Db::name("circle_comment")->where("status",0)->where("circle_info_id",$params)->update(["status"=>1]);
        }
        $this->success("", $result);

    }

    //圈子删除
    public function del()
    {

        $params = input("id");
        $result = Db::name("circle_info")
            ->where("id", $params)
            ->where("user_id", $this->auth->id)
            ->delete();
        $this->success("删除成功", $result);

    }

    //评论发布
    public function comment_add()
    {

        $params = input("post.");
        $params["createtime"] = time();
        $params["user_id"] = $this->auth->id;

        //文字检验
        if (!empty($params["content"])) {
            $req = (new Check)->textCheck($params["content"],"comment_detection");
            if (!empty($req)) {
                $this->error("评论，存在”".$req."“内容");
            }
        }

        $result = false;
        Db::startTrans();
        try {

            $result = Db::name("circle_comment")->insert($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success("发布成功");

    }

    //评论列表
    public function comment_list()
    {
        $circle_info_id = input("circle_info_id");
        $list = Db::name("circle_comment")
            ->alias("a")
            ->field("a.*,u.nickname as user_nickname,u.avatar as user_avatar")
            ->join('user u', 'u.id = a.user_id')
            ->where("circle_info_id", $circle_info_id)
            ->where("a.pid", 0)
            ->order("a.id desc")->select();
        foreach ($list as $k => $v) {
            $list2 = Db::name("circle_comment")
                ->alias("a")
                ->field("a.*,u.nickname as user_nickname,u.avatar as user_avatar,u2.nickname as get_nickname,u2.avatar as get_avatar")
                ->join('user u', 'u.id = a.user_id')
                ->join('user u2', 'u2.id = a.get_id')
                ->where("circle_info_id", $circle_info_id)
                ->where("a.pid", $v["id"])
                ->order("a.id")->select();

            $list[$k]["list"] = $list2;
        }


        $this->success('请求成功', $list);

    }

    //圈子删除
    public function comment_del()
    {

        $params = input("id");
        $result = Db::name("circle_comment")
            ->where("id", $params)
            ->where("user_id", $this->auth->id)
            ->delete();
        $this->success("删除成功", $result);

    }

    //我的圈子 统计
    public function my_circle_count() {


        $detail["value1"] = Db::name("circle_info")->where("status",1)->where("user_id",$this->auth->id)->count();

        $detail["value2"] = Db::name("circle_info")->where("status",0)->where("user_id",$this->auth->id)->count();


        $this->success('请求成功',$detail);
    }

    //私聊统计
    public function chat_add()
    {

        $circle_id = input("circle_id");
        $detail = Db::name("circle_info")
            ->where("id",$circle_id)
            ->find();
        if (empty($detail)) {
            $this->error("圈子不存在");
        }
        Db::name("circle_info")->where("id",$circle_id)->inc("chat_num")->update();
        $car_chat = Db::name("circle_chat")->where([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
        ])->find();

        if (!empty($car_chat)) {
            Db::name("circle_chat")->where("id",$car_chat["id"])->inc("num")->update();
            $this->success("创建成功");
        }


        $car_chat = Db::name("circle_chat")->insert([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
            "num"=>1,
            "createtime"=>time(),
        ]);

        if ($car_chat) {
            $this->success("创建成功");
        } else {
            $this->error("创建失败");
        }

    }

    //电话拨打统计
    public function phone_add()
    {

        $circle_id = input("circle_id");
        $detail = Db::name("circle_info")
            ->where("id",$circle_id)
            ->find();
        if (empty($detail)) {
            $this->error("圈子不存在");
        }
        Db::name("circle_info")->where("id",$circle_id)->inc("dial_num")->update();
        $car_chat = Db::name("circle_phone")->where([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
        ])->find();

        if (!empty($car_chat)) {
            Db::name("circle_phone")->where("id",$car_chat["id"])->inc("num")->update();
            $this->success("创建成功");
        }


        $car_chat = Db::name("circle_phone")->insert([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
            "num"=>1,
            "createtime"=>time(),
        ]);

        if ($car_chat) {
            $this->success("创建成功");
        } else {
            $this->error("创建失败");
        }

    }

    //浏览统计
    public function browse_add()
    {

        $circle_id = input("circle_id");
        $detail = Db::name("circle_info")
            ->where("id",$circle_id)
            ->find();
        if (empty($detail)) {
            $this->error("圈子不存在");
        }
        Db::name("circle_info")->where("id",$circle_id)->inc("browse_num")->update();
        $car_chat = Db::name("circle_browse")->where([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
        ])->find();

        if (!empty($car_chat)) {
            Db::name("circle_browse")->where("id",$car_chat["id"])->inc("num")->update();
            $this->success("创建成功");
        }


        $car_chat = Db::name("circle_browse")->insert([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
            "num"=>1,
            "createtime"=>time(),
        ]);

        if ($car_chat) {
            $this->success("创建成功");
        } else {
            $this->error("创建失败");
        }

    }

    //分享统计
    public function share_add()
    {

        $circle_id = input("circle_id");
        $detail = Db::name("circle_info")
            ->where("id",$circle_id)
            ->find();
        if (empty($detail)) {
            $this->error("圈子不存在");
        }
        Db::name("circle_info")->where("id",$circle_id)->inc("share_num")->update();
        $car_chat = Db::name("circle_share")->where([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
        ])->find();

        if (!empty($car_chat)) {
            Db::name("circle_share")->where("id",$car_chat["id"])->inc("num")->update();
            $this->success("创建成功");
        }


        $car_chat = Db::name("circle_share")->insert([
            "user_id"=>$this->auth->id,
            "get_id"=>$detail["user_id"],
            "circle_id"=>$circle_id,
            "num"=>1,
            "createtime"=>time(),
        ]);

        if ($car_chat) {
            $this->success("创建成功");
        } else {
            $this->error("创建失败");
        }

    }


    //私聊记录
    public function chat_list()
    {

        //分页
        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");
        if (empty($page)) {
            $page = 1;
        }
        $offset = ($page-1)*$limit;

        $circle_id = input("circle_id");
        $list = Db::name("circle_chat")
            ->field("circle_chat.*")
            ->field("u.name,u.nickname,u.shop_name,u.avatar")
            ->join('user u','u.id = circle_chat.user_id')
            ->where("circle_chat.circle_id",$circle_id)
            ->limit("$offset,$limit")
            ->select();
        $this->success("ok",$list);
    }

    //电话拨打记录
    public function phone_list()
    {

        //分页
        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");
        if (empty($page)) {
            $page = 1;
        }
        $offset = ($page-1)*$limit;

        $circle_id = input("circle_id");
        $list = Db::name("circle_phone")
            ->field("circle_phone.*")
            ->field("u.name,u.nickname,u.shop_name,u.avatar")
            ->join('user u','u.id = circle_phone.user_id')
            ->where("circle_phone.circle_id",$circle_id)
            ->limit("$offset,$limit")
            ->select();
        $this->success("ok",$list);
    }

    //浏览记录
    public function browse_list()
    {

        //分页
        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");
        if (empty($page)) {
            $page = 1;
        }
        $offset = ($page-1)*$limit;

        $circle_id = input("circle_id");
        $list = Db::name("circle_browse")
            ->field("circle_browse.*")
            ->field("u.name,u.nickname,u.shop_name,u.avatar")
            ->join('user u','u.id = circle_browse.user_id')
            ->where("circle_browse.circle_id",$circle_id)
            ->limit("$offset,$limit")
            ->select();
        $this->success("ok",$list);
    }

    //分享记录
    public function share_list()
    {

        //分页
        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");
        if (empty($page)) {
            $page = 1;
        }
        $offset = ($page-1)*$limit;

        $circle_id = input("circle_id");
        $list = Db::name("circle_share")
            ->field("circle_share.*")
            ->field("u.name,u.nickname,u.shop_name,u.avatar")
            ->join('user u','u.id = circle_share.user_id')
            ->where("circle_share.circle_id",$circle_id)
            ->limit("$offset,$limit")
            ->select();
        $this->success("ok",$list);
    }


}
